package com.dailybing;

import java.lang.reflect.Field;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

import com.bingarchive.Database;
import com.dailybing.ui.AbstractBrowse;
import com.dailybing.ui.CalendarBrowse;

public class MainActivity extends Activity {

	private ViewGroup mvgWindow = null;
	private AbstractBrowse mabForm = null;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main_frame);

		ActionBar actionBar = this.getActionBar();
		actionBar.setIcon(R.drawable.bingicon);
		showActionOverFlow();
				
		Date dt = new Date(23, 1, 31);
		
//		mabForm = new OneByOneBrowse(this);
		mabForm = new CalendarBrowse(this);
		mvgWindow = (ViewGroup) this.findViewById(R.id.vgBrowseWindow);
		mvgWindow.addView(mabForm.getBrowerView());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_CleanDatabase:
			Database.OpenDatabase(this);
			Database.O.cleanRecord();
			Database.CloseDatabase();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	private void showActionOverFlow() {
		try {
			ViewConfiguration mconfig = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			if (menuKeyField != null) {
				menuKeyField.setAccessible(true);
				menuKeyField.setBoolean(mconfig, false);
			}
		} catch (Exception ex) {
		}
	}
}
